#include "sessiondetailwidget.h"
#include <QVBoxLayout>
#include <QFontMetrics>
#include <QGridLayout>
#include "debug.h"
#include "choosefriendwidget.h"
#include <QMessageBox>
#include "model/datacenter.h"

using namespace model;

/**********************************************************************************************
 * 会话详情的弹窗
 **********************************************************************************************/
SessionDetailWidget::SessionDetailWidget(QWidget *parent,const model::UserInfo& userInfo)
    :QDialog(parent)
    ,userInfo(userInfo)
{
    //1.设置基本属性
    this->setFixedSize(400,300);
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/SessionDetail.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);


    //2.布局
    QGridLayout* glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(50,0,50,0);
    this->setLayout(glayout);

    //3.添加“创建群聊按钮”
    AvatarItem* creatGroutBtn = new AvatarItem(QIcon(":/resource/add.png"),"添加");
    glayout->addWidget(creatGroutBtn,0,0);

#ifdef TEST_UI
    //4.添加当前用户按钮
    AvatarItem* currentUser = new AvatarItem(QIcon(":/resource/lion.png"),"张三123456");
    glayout->addWidget(currentUser,0,1);
#endif

    //添加当前用户到界面上
    AvatarItem* currentUser = new AvatarItem(userInfo.avatar,userInfo.nickName);
    glayout->addWidget(currentUser,0,1);

    //5.删除好友按钮
    QPushButton* deleteFrientBtn = new QPushButton();
    deleteFrientBtn->setFixedHeight(50);
    deleteFrientBtn->setText("删除好友");
    QString style = "QPushButton{border:1px solid rgb(90,90,90); border-radius:5px;}";
    style += "QPushButton:pressed{background-color:rgb(235,235,235);}";
    deleteFrientBtn->setStyleSheet(style);
    glayout->addWidget(deleteFrientBtn,1,0,1,3);


    //给+号添加槽函数
    connect(creatGroutBtn->getAvatar(),&QPushButton::clicked,this,[=](){
        ChooseFriendWidget* chooseFriendWidget = new ChooseFriendWidget(this,userInfo.userId);
        chooseFriendWidget->exec();
    });

    //给删除好友添加信号槽
    connect(deleteFrientBtn,&QPushButton::clicked,this,&SessionDetailWidget::clickDeleteFriendBtn);
}

void SessionDetailWidget::clickDeleteFriendBtn()
{
    auto result = QMessageBox::warning(this,"确认删除","确认删除该好友?",QMessageBox::Ok | QMessageBox::Cancel);
    if(result != QMessageBox::Ok)
    {
        LOG() << "用户取消删除";
        return;
    }
    //发送删除好友的请求
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(this->userInfo.userId);
    this->close();
}


/**********************************************************************************************
 * 会话详情中的自定义控件: 用户头像+姓名
 **********************************************************************************************/
AvatarItem::AvatarItem(const QIcon &avatar, const QString &name)
{
    //1.创建基本布局
    this->setFixedSize(70,80);
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(20);
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setAlignment(Qt::AlignCenter);
    this->setLayout(vlayout);

    //2.创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40,40);
    avatarBtn->setIconSize(QSize(40,40));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton {border: none};");

    //3. 创建名字
    nameLabel = new QLabel();
    nameLabel->setText(name);
    QFont font("微软雅黑", 10);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);


    //4.对姓名的长度做截断处理
    QFontMetrics metrics(font);
    int textWidth = metrics.horizontalAdvance(name);
    const int MAX_WIDTH = 65;
    if(textWidth >= MAX_WIDTH)
    {
        //需要截断
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = name.size() * ((double)availableWidth / textWidth);  //只取name的前几个字符
        QString newName = name.left(availableSize);
        nameLabel->setText(newName + tail);
    }

    //5.添加至布局管理器中
    vlayout->addWidget(avatarBtn);
    vlayout->addWidget(nameLabel);
}










